<template>
  <div class="search-section">
    <!-- 搜索标题 -->
    <div class="search-header">
      <h3 class="search-title">{{ postZone.mainPost?.title || '"出闲置🔍"' }}</h3>
    </div>
    
    <div class="content-layout">
      <!-- 左侧用户帖子卡片 -->
      <div class="post-card" @click="handlePostClick">
        <!-- 只有当mainPost存在时才渲染 -->
        <div v-if="postZone.mainPost" class="post-header">
          <div class="user-info">
            <img 
              :src="postZone.mainPost.userAvatar" 
              :alt="postZone.mainPost.userName" 
              class="user-avatar"
            />
            <div class="user-details">
              <span class="user-name">{{ postZone.mainPost.userName }}</span>
            </div>
          </div>
        </div>
        
        <div v-if="postZone.mainPost" class="post-content">
          <p class="post-text">{{ postZone.mainPost.postContent }}</p>
        </div>
        
        <!-- 关注者数量 -->
        <div v-if="postZone.mainPost" class="follower-info">
          <span class="follower-count">{{ formatFollowerCount(postZone.mainPost.followerNum) }}个小伙</span>
        </div>
        
        <!-- 新增：渲染contentBlockList图片 -->
        <div v-if="postZone.contentBlockList && postZone.contentBlockList.length > 0" class="image-gallery">
          <div 
            v-for="(block, index) in postZone.contentBlockList" 
            :key="index"
            class="gallery-image-container"
          >
            <img 
              :src="block.img" 
              :alt="`帖子图片${index + 1}`" 
              class="gallery-image"
              @error="handleImageError($event)"
            />
          </div>
        </div>
      </div>
      
      <!-- 右侧福利区域 -->
      <div class="benefits-sidebar">
        <div class="benefit-item benefit-gift">
          <div class="benefit-content">
            <div class="benefit-title">谷粒福利</div>
            <div class="benefit-subtitle">#福利介绍文字</div>
          </div>
          <div class="benefit-icon">🎁</div>
        </div>
        
        <div class="benefit-item benefit-moment">
          <div class="benefit-content">
            <div class="benefit-title">谷粒瞬间</div>
            <div class="benefit-subtitle">#合租达人万象</div>
          </div>
          <div class="benefit-icon">🏝️</div>
        </div>
        
        <div class="benefit-item benefit-life">
          <div class="benefit-content">
            <div class="benefit-title">谷粒生活馆</div>
            <div class="benefit-subtitle">#合租达人万象</div>
          </div>
          <div class="benefit-icon">📷</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'

const props = defineProps({
  postZone: {
    type: Object,
    default: () => ({})
  }
})

const formatFollowerCount = (num) => {
  if (!num) return '0'
  if (num >= 1000) {
    return Math.floor(num / 1000) + 'k'
  }
  return num.toString()
}

const handlePostClick = () => {
  ElMessage.info('查看帖子详情')
}

// 处理图片加载错误
const handleImageError = (event) => {
  console.warn('图片加载失败，使用默认图片:', event.target.src)
  event.target.src = '/src/assets/imgs/blank.png' // 使用默认占位图
}
</script>

<style scoped>
.search-section {
  padding: 0 16px 20px;
}

.search-header {
  margin-bottom: 16px;
}

.search-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.content-layout {
  display: grid;
  grid-template-columns: 1fr 120px;
  gap: 16px;
  align-items: start;
}

.post-card {
  background-color: #fff;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s ease;
}

.post-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.post-header {
  margin-bottom: 12px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 12px;
  object-fit: cover;
  border: 2px solid #f0f0f0;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 14px;
  font-weight: 600;
  color: #333;
}

.post-content {
  margin-bottom: 12px;
}

.post-text {
  font-size: 14px;
  color: #333;
  line-height: 1.5;
  margin: 0;
}

.follower-info {
  margin-top: 8px;
}

.follower-count {
  font-size: 12px;
  color: #999;
}

/* 图片画廊样式 */
.image-gallery {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
  gap: 6px;
}

.gallery-image-container {
  border-radius: 8px;
  overflow: hidden;
  background-color: #f5f5f5;
}

.gallery-image {
  width: 100%;
  height: 80px;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.gallery-image:hover {
  transform: scale(1.05);
}

.benefits-sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.benefit-item {
  background-color: #fff;
  border-radius: 8px;
  padding: 10px 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border: 1px solid #f0f0f0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 50px;
}

.benefit-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-color: #e0e0e0;
}

.benefit-content {
  flex: 1;
  text-align: left;
}

.benefit-title {
  font-size: 12px;
  font-weight: 600;
  color: #333;
  margin-bottom: 2px;
}

.benefit-subtitle {
  font-size: 10px;
  color: #999;
  line-height: 1.2;
}

.benefit-icon {
  font-size: 16px;
  margin-left: 8px;
}

/* 不同福利项的特殊样式 */
.benefit-gift {
  background: linear-gradient(135deg, #ffe8e8 0%, #fff0f0 100%);
}

.benefit-moment {
  background: linear-gradient(135deg, #e8f5ff 0%, #f0f8ff 100%);
}

.benefit-life {
  background: linear-gradient(135deg, #f0fff0 0%, #f8fff8 100%);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-section {
    padding: 0 12px 16px;
  }
  
  .content-layout {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .benefits-sidebar {
    flex-direction: row;
    gap: 8px;
    overflow-x: auto;
  }
  
  .benefit-item {
    flex-shrink: 0;
    min-width: 100px;
    padding: 8px 6px;
    flex-direction: column;
    text-align: center;
  }
  
  .benefit-content {
    text-align: center;
  }
  
  .benefit-icon {
    margin-left: 0;
    margin-top: 4px;
  }
  
  .search-title {
    font-size: 16px;
  }
  
  .post-card {
    padding: 12px;
  }
  
  .user-avatar {
    width: 32px;
    height: 32px;
    margin-right: 8px;
  }
  
  .user-name {
    font-size: 13px;
  }
  
  .follower-count {
    font-size: 11px;
  }
  
  .post-text {
    font-size: 13px;
  }
}
</style>
